package practise.alogrithm.hundredInterviewQuestions;

import java.util.Stack;

/**
 * Created by richard on 6/3/14.
 */
public class QueueImplByStack<T> {
    Stack<T> stack1;
    Stack<T> stack2;
    public QueueImplByStack(){
        stack1= new Stack<>();
        stack2 = new Stack<>();
    }
    public void offer(T t){
        if(stack1.isEmpty()&&stack2.isEmpty()){
            stack1.push(t);
        }
        else if(stack1.isEmpty()){
            stack1.push(t);
        }
        else stack2.push(t);
    }

    public T pool(){
        if(stack1.isEmpty()&&stack2.isEmpty()){
            throw new NullPointerException();
        }
        else if(stack1.isEmpty()){
            while (!stack2.isEmpty()){
                stack1.push(stack2.pop());

            }
            return stack1.pop();
        }
        else while (!stack2.isEmpty()){
                stack1.push(stack2.pop());
            }
        return stack2.pop();
    }
}
